<?php
/**
 * 获取请求参数基类
 */

namespace diablo\lib;


class Request
{
    public $get;
    public $post;
    public $param;
    public $server;
    public $_server;
    public $ip;

    public function __construct()
    {
        $this->get = $_GET;
        $this->post = $_POST;
        $this->param = $_REQUEST;
        $this->server = $this->server();
        $this->_server = $_SERVER;
        $this->ip = $this->getIP();
    }

    private function server()
    {
        return [
            'server_name' => $_SERVER['SERVER_NAME'],
            'route' => $_SERVER['REQUEST_URI'],
            'ip' => $this->getIP(),
            'server_ip' => $_SERVER['SERVER_ADDR'],
            'getParam' => $_SERVER['QUERY_STRING']
        ];
    }

    //获取IP
    private function getIP()
    {
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            } elseif (getenv("HTTP_CLIENT_IP")) {
                $ip = getenv("HTTP_CLIENT_IP");
            } else {
                $ip = getenv("REMOTE_ADDR");
            }
        }
        return $ip;
    }
}